package com.dcloud.manage.dblib.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dcloud.manage.dblib.entity.request.*;
import com.dcloud.manage.dblib.entity.vo.DatalibLogicTableVO;
import com.dcloud.manage.dblib.entity.vo.DatalibTableExtVO;
import net.sf.saxon.functions.ConstantFunction;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.dcloud.manage.dblib.entity.DatalibLogicTable;
import com.dcloud.manage.dblib.service.IDatalibLogicTableService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: datalib_logic_table
 * @Author: jeecg-boot
 * @Date:   2024-03-11
 * @Version: V1.0
 */
@Api(tags="数据资产表管理")
@RestController
@RequestMapping("/api/datalibLogicTable/v1/")
@Slf4j
public class DatalibLogicTableController extends JeecgController<DatalibLogicTable, IDatalibLogicTableService> {
	@Autowired
	private IDatalibLogicTableService datalibLogicTableService;
	
	/**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "datalib_logic_table-分页列表查询")
	@ApiOperation(value="datalib_logic_table-分页列表查询", notes="datalib_logic_table-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(DatalibLogicTablePageReq datalibLogicTablePageReq,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<DatalibLogicTableVO> page = new Page<DatalibLogicTableVO>(pageNo, pageSize);
		IPage<DatalibLogicTableVO> pageList = datalibLogicTableService.selectPage(page, datalibLogicTablePageReq);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加成功后返回主键
	 *
	 * @param datalibLogicTable
	 * @return
	 */
	@AutoLog(value = "datalib_logic_table-添加")
	@ApiOperation(value="datalib_logic_table-添加", notes="datalib_logic_table-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DatalibLogicTable datalibLogicTable) {
		Integer id = datalibLogicTableService.saveReturnId(datalibLogicTable);
		return Result.OK(id);
	}
	
	/**
	 *  编辑
	 *
	 * @param datalibLogicTable
	 * @return
	 */
	@AutoLog(value = "datalib_logic_table-编辑")
	@ApiOperation(value="datalib_logic_table-编辑", notes="datalib_logic_table-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DatalibLogicTable datalibLogicTable) {
		datalibLogicTableService.updateById(datalibLogicTable);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "datalib_logic_table-通过id删除")
	@ApiOperation(value="datalib_logic_table-通过id删除", notes="datalib_logic_table-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		datalibLogicTableService.removeByIdAFieldMap(id);
		return Result.OK("删除成功!");
	}
	

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "datalib_logic_table-通过id查询")
	@ApiOperation(value="datalib_logic_table-通过id查询", notes="datalib_logic_table-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DatalibLogicTable datalibLogicTable = datalibLogicTableService.getById(id);
		if(datalibLogicTable==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(datalibLogicTable);
	}

	 /**
	  * 根据表id查询表扩展信息
	  *
	  * @param tableId 资源表ID
	  * @return
	  */
	 @AutoLog(value = "根据表id查询表扩展信息")
	 @ApiOperation(value="根据表id查询表扩展信息", notes="根据表id查询表扩展信息")
	 @GetMapping(value = "/queryExtInfoById")
	 public Result<?> queryExtInfoById(@RequestParam(name="tableId",required=true) Integer tableId) {
		 DatalibTableExtVO datalibTableExtVO = null;
		 try {
			 datalibTableExtVO = datalibLogicTableService.queryExtInfoById(tableId);
		 } catch (Exception e) {
			 log.error(e.getMessage());
			 Result.error(e.getMessage());
		 }

		 return Result.OK(datalibTableExtVO);
	 }

	 /**
	  * 修改源表扩展信息
	  *
	  * @param updateTableExtReq 扩展表信息
	  * @return
	  */
	 @AutoLog(value = "修改源表扩展信息")
	 @ApiOperation(value="修改源表扩展信息", notes="修改源表扩展信息")
	 @PostMapping(value = "/updateExtInfo")
	 public Result<?> updateExtInfo(@RequestBody UpdateTableExtReq updateTableExtReq) {
		 try {
			 datalibLogicTableService.updateExtInfo(updateTableExtReq);
		 } catch (Exception e) {
			 log.error(e.getMessage());
			 Result.error(e.getMessage());
		 }

		 return Result.OK("修改成功");
	 }

	 /**
	  * 数据资产表发布
	  *
	  * @param datalibTablePublishReq 数据资产表发布请求参数
	  * @return
	  */
	 @AutoLog(value = "数据资产表发布")
	 @ApiOperation(value="数据资产表发布", notes="数据资产表发布")
	 @PostMapping(value = "/publish")
	 public Result<?> publish(@RequestBody DatalibTablePublishReq datalibTablePublishReq) {
		 try {
			 datalibLogicTableService.publish(datalibTablePublishReq);
		 } catch (Exception e) {
			 log.error(e.getMessage());
			 return Result.error(e.getMessage());
		 }

		 return Result.OK("发布成功", true);
	 }


	 /**
	  * 数据资产表即席查询
	  *
	  * @return
	  */
	 @AutoLog(value = "数据资产表即席查询")
	 @ApiOperation(value="数据资产表即席查询", notes="数据资产表即席查询")
	 @PostMapping(value = "/jxcx")
	 public Result<?> queryJXCX(@RequestBody DatalibJXCXReq datalibJXCXReq) {
		 try {
			 Map<String, Object> list = datalibLogicTableService.jxcx(datalibJXCXReq);
			 return Result.OK(list);
		 }catch (Exception e){
			 log.error(e.getMessage());
			 return Result.error(e.getMessage());
		 }
	 }

	 /**
	  * 数据表DDL信息
	  *
	  * @return
	  */
	 @AutoLog(value = "数据表DDL信息")
	 @ApiOperation(value="数据表DDL信息", notes="数据表DDL信息")
	 @PostMapping(value = "/innerTableInfo")
	 public Result<?> innerTableInfo(@RequestBody DatalibInnerTableInfoReq datalibInnerTableInfoReq	) {
		 try {
			 String DDLSQL = datalibLogicTableService.innerTableInfo(datalibInnerTableInfoReq);
			 return Result.OK("success",DDLSQL);
		 }catch (Exception e){
			 log.error(e.getMessage());
			 return Result.error(e.getMessage());
		 }
	 }

}
